home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8376 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  78 lines

  1. Path: qns2.qns.com!not-for-mail
  2. From: mjarvis@qns2.qns.com (Michael Jarvis)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Why is Gets() so bad?
  5. Date: 3 Mar 1996 14:29:03 -0600
  6. Organization: Questar Network Services
  7. Message-ID: <4hcvef$308@qns2.qns.com>
  8. References: <4hb1ie$pa7@ixnews2.ix.netcom.com>
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Studcat's Big Studdog. (Studcat's Big Studdog. (studwoof@ix.netcom.com)) wrote:
  12. > Hi..  I'm not a newbie..  I don't even use Gets(), fgets(), or scanf.. 
  13. > I use my own getstring function.  My question is that everywhere I see
  14. > that gets() is bad.  Why exactly is it so bad?  Could someone please
  15. > explain in more detail than what the FAQ does?  Thanks.
  16.  
  17. Let's say you have the following bit of code:
  18. ----------------------------------------------------------------------
  19. #include <stdio.h>
  20. #include <errno.h>
  21.  
  22. int main( int argc, char *argv[] )
  23. {
  24.         char buf[20];
  25.  
  26.         puts( "Please enter some text and press ENTER." );
  27.         puts( "         11111111112" );
  28.         puts( "12345678901234567890" );
  29.  
  30.         if ( gets( buf ) == NULL )
  31.                 perror( "Error reading in gets()");
  32.  
  33.         printf( "The buf=\"%s\"\n", buf );
  34.  
  35.         return 0;
  36. }
  37. ----------------------------------------------------------------------
  38.  
  39. The function gets() will blindly read in whatever you type on stdin.
  40. If you type in more data than it's able to handle (ie more than 20
  41. bytes in this case) then you've written beyond the boundaries of buf
  42. and Bad Things will happen.
  43.  
  44. A better solution would be:
  45. ----------------------------------------------------------------------
  46. #include <stdio.h>
  47. #include <errno.h>
  48.  
  49. int main( int argc, char *argv[] )
  50. {
  51.         char buf[20];
  52.  
  53.         puts( "Please enter some text and press ENTER." );
  54.         puts( "         11111111112" );
  55.         puts( "12345678901234567890" );
  56.  
  57.         if ( fgets( buf, sizeof(buf)-1, stdin ) == NULL )
  58.                 perror( "Error reading in gets()");
  59.  
  60.         printf( "The buf=\"%s\"\n", buf );
  61.  
  62.         return 0;
  63. }
  64. ----------------------------------------------------------------------
  65.  
  66. By using fgets you can tell it the maximum amount of data to read.  You
  67. could type megs and megs of data but it would only read 19 bytes (leaving
  68. room for the '\0' at the end).
  69.  
  70. Using gets() is just asking for trouble, in my opinion.
  71.  
  72.                                               -michael
  73. -- 
  74. Michael Jarvis   |  Finger for PGP Public key  |   QNSnet Technical Support
  75. mjarvis@qns.com  | http://www.qns.com/~mjarvis |   Questar Network Services
  76. GC3.1: GCS d s+++: a26 C++++ USLV++++$ P++++ L++ E--- W++ N++ !o K+ W-- !O 
  77. M- !V PS+ PE Y+ PGP+ t+ 5 X R tv b+++ DI+++ D++ G+ e>++ h---(*) r+++ y+++
  78.